package com.lq.tech.infra;

import com.lq.tech.feign.AuthClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by john on 2017/4/11.
 */
public class BasicController {
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private AuthClient authClient;

    public <T> ActionResult<T> actionResult(ErrorCode code, T value) {
        return new ActionResult<T>(code.getCode(), code.getDesc(), value);
    }

    public <T> ActionResult<T> actionResult(T value) {
        ErrorCode code = ErrorCode.Success;
        return actionResult(code, value);
    }

    public ActionResult actionResult(ErrorCode code) {
        return actionResult(code, null);
    }

    public String getUserId() {
        String userId = request.getHeader("x-user-id");
        if (StringUtils.isNotBlank(userId)) {
            return userId;
        }
        return "";
    }

//    /**
//     * 获取当前登录人姓名
//     *
//     * @return
//     */
//    public String getUserName() {
//        String userName = "";
//        String userId = getUserId();
//        try {
//            ActionResult dd = authClient.getUserDetail(userId);
//            UserCacheDTO userInfo = (UserCacheDTO) dd.getValue();
//            if (userInfo != null) {
//                userName = userInfo.getName();
//            }
//        } catch (Exception e) {
//            userName = userId;
//        }
//        return userName;
//    }
}
